package com.loopj.android.http;

import android.content.Context;
import android.util.Log;

import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HttpContext;

/**
 * Processes http requests in synchronous mode, so your caller thread will be
 * blocked on each request
 * 
 * @see com.loopj.android.http.AsyncHttpClient
 */
public class SyncHttpClient extends AsyncHttpClient {

	/**
	 * Creates a new SyncHttpClient with default constructor arguments values
	 */
	public SyncHttpClient() {
		super(false, 80, 443);
	}

	/**
	 * Creates a new SyncHttpClient.
	 * 
	 * @param httpPort
	 *            non-standard HTTP-only port
	 */
	public SyncHttpClient(int httpPort) {
		super(false, httpPort, 443);
	}

	/**
	 * Creates a new SyncHttpClient.
	 * 
	 * @param httpPort
	 *            non-standard HTTP-only port
	 * @param httpsPort
	 *            non-standard HTTPS-only port
	 */
	public SyncHttpClient(int httpPort, int httpsPort) {
		super(false, httpPort, httpsPort);
	}

	/**
	 * Creates new SyncHttpClient using given params
	 * 
	 * @param fixNoHttpResponseException
	 *            Whether to fix or not issue, by ommiting SSL verification
	 * @param httpPort
	 *            HTTP port to be used, must be greater than 0
	 * @param httpsPort
	 *            HTTPS port to be used, must be greater than 0
	 */
	public SyncHttpClient(boolean fixNoHttpResponseException, int httpPort,
			int httpsPort) {
		super(fixNoHttpResponseException, httpPort, httpsPort);
	}

	/**
	 * Creates a new SyncHttpClient.
	 * 
	 * @param schemeRegistry
	 *            SchemeRegistry to be used
	 */
	public SyncHttpClient(SchemeRegistry schemeRegistry) {
		super(schemeRegistry);
	}

	@Override
	protected RequestHandle sendRequest(DefaultHttpClient client,
			HttpContext httpContext, HttpUriRequest uriRequest,
			String contentType, ResponseHandlerInterface responseHandler,
			Context context) {
		if (contentType != null) {
			uriRequest.addHeader("Content-Type", contentType);

			Log.i("sendRequest", contentType);
		}

		responseHandler.setUseSynchronousMode(true);

		/*
		 * will execute the request directly
		 */
		new AsyncHttpRequest(client, httpContext, uriRequest, responseHandler)
				.run();

		// Return a Request Handle that cannot be used to cancel the request
		// because it is already complete by the time this returns
		return new RequestHandle(null);
	}
}
